cmake_minimum_required (VERSION 2.8)
project (libwebsocket)

SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib)

# c++11 support
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11")

set(LIB_LIST)

# cppzmq
find_path(CPPZMQ_INCLUDE_DIRS NAMES zmq.hpp HINTS "deps/cppzmq")
find_library(CPPZMQ_LIBRARIES NAMES zmq)
if (CPPZMQ_INCLUDE_DIRS AND CPPZMQ_LIBRARIES)
    include_directories("${CPPZMQ_INCLUDE_DIRS}")
    list(APPEND LIB_LIST "${CPPZMQ_LIBRARIES}")
endif()

# forestdb
find_path(FORESTDB_INCLUDE_DIRS NAMES forestdb.h HINTS "deps/forestdb/include/libforestdb")
find_library(FORESTDB_LIBRARIES NAMES forestdb HINTS "lib")
if (FORESTDB_INCLUDE_DIRS AND FORESTDB_LIBRARIES)
    include_directories("${FORESTDB_INCLUDE_DIRS}")
    link_directories("${FORESTDB_LIBRARIES}")
    list(APPEND LIB_LIST ${FORESTDB_LIBRARIES})
endif()

# uWebsockets
find_path(UWS_INCLUDE_DIRS NAMES uWS.h HINTS "deps/uWebsockets/src")
find_library(UWS_LIBRARIES NAMES uWS HINTS "lib")
if (UWS_INCLUDE_DIRS AND UWS_LIBRARIES)
    include_directories("${UWS_INCLUDE_DIRS}")
    list(APPEND LIB_LIST ${UWS_LIBRARIES})
endif()

# wren
find_path(WREN_INCLUDE_DIRS NAMES wren.h HINTS "deps/wren/src/include")
find_path(WREN_INCLUDE_DIRS_2 NAMES vm.h HINTS "deps/wren/src/cli")
find_library(WREN_LIBRARIES NAMES wren HINTS "lib")
if (WREN_INCLUDE_DIRS AND WREN_INCLUDE_DIRS_2 AND WREN_LIBRARIES)
    include_directories("${WREN_INCLUDE_DIRS}")
    include_directories("${WREN_INCLUDE_DIRS_2}")
    list(APPEND LIB_LIST ${WREN_LIBRARIES})
endif()

INCLUDE_DIRECTORIES(include)
file(GLOB_RECURSE DIR_SRCS "src/*.cc")
file(GLOB_RECURSE DIR_MAINS "main/*.cc")

# library
#ADD_LIBRARY(websocket STATIC ${DIR_SRCS})
#TARGET_LINK_LIBRARIES(websocket ${LIB_LIST})

# application
add_executable(main ${DIR_SRCS} ${DIR_MAINS})
TARGET_LINK_LIBRARIES(main ${LIB_LIST} -lssl -lz)

# cpplint
file(GLOB_RECURSE ALL_CHECK_FILES "include/*.h" "src/*.cc" "main/*.cc")

add_custom_target(
        cpplint ALL
        COMMAND cpplint
        ${ALL_CHECK_FILES}
)

# cppcheck
add_custom_target(
        cppcheck ALL
        COMMAND cppcheck
        --enable=all
        --std=c++11
        --verbose
        --quiet
        ${ALL_CHECK_FILES}
)

# valgrind
find_program(VALGRIND "valgrind")
if(VALGRIND)
    add_custom_target(valgrind
        COMMAND "${VALGRIND}" --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes $<TARGET_FILE:main>)
endif()

# test
add_subdirectory(tests)

# coverage
OPTION (ENABLE_GCOV "Enable gcov (debug, Linux builds only)" ON)
IF(ENABLE_GCOV AND UNIX)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
    SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
    list(APPEND LIB_LIST gcov)
    set(CMAKE_BUILD_TYPE Debug)
    set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/deps/cmake)
    include(coverage)
    set(TESTS unittest)
    setup_target_for_coverage(coverage ${TESTS} coverage)
ENDIF()
